using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

using AGG;
using AGG.Image;
using AGG.VertexSource;
using AGG.Transform;
using AGG.UI;

using Reflexive.Math;
using Reflexive.Game;
using Reflexive.Graphics;

namespace RockBlaster
{
    public class PlayfieldView : GUIWidget
    {
        Playfield m_Playfield;

        public delegate void MenuEventHandler(GUIWidget button);
        public event MenuEventHandler Menu;

        public PlayfieldView(rect_d bounds)
        {
            Bounds = bounds;

            ImageSequence menuButtonSequence = (ImageSequence)DataAssetCache.Instance.GetAsset(typeof(ImageSequence), "MenuButtonFromGame");
            ButtonWidget menuButton = new ButtonWidget(400, 12, new ThreeImageButtonView(menuButtonSequence.GetImageByIndex(0), menuButtonSequence.GetImageByIndex(1), menuButtonSequence.GetImageByIndex(2)));
            AddChild(menuButton);
            menuButton.Click += new ButtonWidget.ButtonEventHandler(EscapeMenu);
        }

        private void EscapeMenu(object sender, MouseEventArgs mouseEvent)
        {
            if (Menu != null)
            {
                Menu(this);
            }
        }

        public void StartOnePlayerGame()
        {
            Focus();

            m_Playfield = new Playfield();

            m_Playfield.StartOnePlayerGame();

            //m_Playfield.SaveXML("Test");
        }

        internal void StartTwoPlayerGame()
        {
            Focus();

            m_Playfield = new Playfield();

            m_Playfield.StartTwoPlayerGame();
        }

        internal void StartFourPlayerGame()
        {
            Focus();

            m_Playfield = new Playfield();

            m_Playfield.StartFourPlayerGame();
        }

        public override void OnDraw(Graphics2D graphics2D)
        {
            ImageSequence background = (ImageSequence)DataAssetCache.Instance.GetAsset(typeof(ImageSequence), "GameBackground");
            graphics2D.Render(background.GetImageByIndex(0), 0, 0);

            m_Playfield.Draw(graphics2D);

            base.OnDraw(graphics2D);
        }

        public override void OnKeyDown(AGG.UI.KeyEventArgs keyEvent)
        {
            foreach (Player aPlayer in m_Playfield.PlayerList)
            {
                aPlayer.KeyDown(keyEvent);
            }

            if (keyEvent.Control && keyEvent.KeyCode == Keys.S)
            {
                m_Playfield.SaveXML("TestSave");
            }

            base.OnKeyDown(keyEvent);
        }

        public override void OnKeyUp(AGG.UI.KeyEventArgs keyEvent)
        {
            foreach (Player aPlayer in m_Playfield.PlayerList)
            {
                aPlayer.KeyUp(keyEvent);
            }
            base.OnKeyUp(keyEvent);
        }

        public void OnUpdate(double NumSecondsPassed)
        {
            m_Playfield.Update(NumSecondsPassed);
        }
    }
}
